<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


/**
 * 数组去重
 * @param $arr
 * @return mixed
 */
function array_filter_full($arr){
    foreach($arr as $key=>$value){
        if($value ===''){
            unset($arr[$key]);
        }
    }
    return $arr;
}


/**
 * 随机产生安全码
 * @param $len int 长度
 * @param $type int 类型 1全部字母数字  2数字  3小写字母  4大写字母
 * @return string
 */
function getCode($len,$type=1){
    $code = '';
    switch($type){
        case 1:
            $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
            break;
        case 2:
            $str = '0123456789';
            break;
        case 3:
            $str = 'abcdefghijklmnopqrstuvwxyz';
            break;
        case 4:
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 5:
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            break;
        default:
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    }
    for($i=0;$i<$len;$i++){
        $code .= $str[mt_rand(0,mb_strlen($str,'utf-8') -1 )] ;
    }
    return $code;
}

/**
 * 密码加密算法
 * @author lixiaoming
 * @time 2017/11/16
 * @param string $pwd 明文密码
 * @param string $username 用户名
 * @param string $encrypt 随机安全码
 * @return string 32位加密字符串
 */
function getMd5($pwd,$username,$encrypt){
    $username = strtolower($username);
    return md5(md5($pwd).$username.$encrypt);
}

/**
 * 获取真实IP
 * @return array|false|string
 */
function get_ip() {
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
    else
        if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else
            if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
                $ip = getenv("REMOTE_ADDR");
            else
                if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
                    $ip = $_SERVER['REMOTE_ADDR'];
                else
                    $ip = "unknown";
    return ($ip);
}

/**
 * 获取文件扩展名
 * @param string $file 文件路径
 * @return string txt/jpg
 * */
function getExtension($file=''){
    if($file){
        return pathinfo($file, PATHINFO_EXTENSION);
    }
}


/**
 * 转换单位 支持 TB/GB/MB/KB/B  不限大小写 转换为字节
 * @param string $size 文件大小 例：100MB
 * @return int 1024000
 */
function tfFilesize($size=''){
    if(empty($size)){
        return 0;
    }

    //1、获取大小数值
    $sizenum = floatval($size);
    if(empty($sizenum)){
        return 0;
    }

    //2、根据大小数值获取后面单位
    $cm = explode($sizenum,$size);
    $cm = !empty($cm[1])?strtolower(trim($cm[1])):'';
    $res = 0;
    $base= 1024;
    switch ($cm){
        case 'tb':
            $res = $sizenum*$base*$base*$base*$base;
            break;
        case 'gb':
            $res = $sizenum*$base*$base*$base;
            break;
        case 'mb':
            $res = $sizenum*$base*$base;
            break;
        case 'kb':
            $res = $sizenum*$base;
            break;
        case 'b':
            $res = $sizenum;
            break;
        default:
            break;
    }

    return ceil($res);
}

/**
 * 权限递归循环取得无限分级
 * @param $arr 处理数组
 * @param int $pid 父级键
 * @return array 返回
 */
function grade($arr,$pid=0){
    $pgrade = array();//定义一个空数组
    foreach ($arr as $item) {//遍历输出数组内容
        if ($item['pid'] == $pid) {//父id与内容ID相同
            $item['children'] = grade($arr, $item['id']);//继续调用本身
            if (empty($item['children'])) {//如果子内容为空，
                unset($item['children']);//销毁指定的变量
            }
            $pgrade[] = $item;//否则输出$item内容
        }
    }
    return $pgrade;//循环结束，返回数组内容
}

